package com.wanghlme.day27.web;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.wanghlme.day27.filter.EncodingFilter;
import com.wanghlme.day27.pojo.Dept;
import com.wanghlme.day27.pojo.Emp;
import com.wanghlme.day27.service.ICompanyService;
import com.wanghlme.day27.service.impl.CompanyServiceImpl;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

@WebServlet("/listAllDept")
public class ListAllDeptAction extends HttpServlet {
    private static final Logger logger = LoggerFactory.getLogger(EncodingFilter.class.getName());
    private ICompanyService companyService;

    public ListAllDeptAction() {
        companyService = new CompanyServiceImpl();
    }

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        List<Dept> list = companyService.listAllDept();
        logger.info("List size is {}", list.size());
        PrintWriter out = resp.getWriter();
        String listStr = JSONArray.toJSONString(list);
        out.print(listStr);
        logger.info("List is \n {}", listStr);
        out.flush();
        out.close();
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doGet(req, resp);
    }
}
